class EstimationsController < ApplicationController

  def index
    project_id  = (session[:selected_project] || 0).to_i
    resp_id     = (session[:selected_resp] || 0).to_i
    sel_task    = (session[:selected_task] || 0).to_i

    cond = "(progress=#{PROGRESS_TODO} or progress=#{PROGRESS_BEGUN})"
    user = User.find(session['user'])
    user_projects = user.active_projects_ids.join(',')
    if user_projects != ''
      if project_id.to_i != 0
        cond += " and project_id='#{project_id}'"
      else
        cond += " and project_id in (#{user_projects})"
      end
      if resp_id.to_i != 0
        cond += " and user_id='#{resp_id}'"
      end  
    else
      cond += " and user_id='#{user.id}'"
    end

    cond += " and (state='next' or state='maybe')"  
    cond += " and orig_est is null"
    cond += " and task_id='#{sel_task}'"

    order = "`order`"
    inc = ["user","tags","project"]
    @tasks = Task.find(:all, :conditions=>cond, :include=>inc, :order=>order)
    @tasks = @tasks.sort_by {|t| -t.subremains_or_zero}
  end

end
